package com.tjetc.service.Impl;

import com.tjetc.common.JsonResult;
import com.tjetc.dao.AreaMapper;
import com.tjetc.entity.Area;
import com.tjetc.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    /**
     * 父id获取到所有的子区域
     * @param Pid
     * @param type
     * @return
     */
    @Override
    public JsonResult<List<Area>> getAllChildByPid(Integer Pid, Integer type) {
        List<Area> cities = areaMapper.searchChildAreaById(Pid);
        System.out.println(cities);
        if(cities!=null){
            return new JsonResult(0,"查询成功",cities);
        }
        else {
            return new JsonResult(1,"查询失败",cities);
        }
    }

    /**
     * 区域名称获取区域信息
     * @param name 输入city的名称
     * @return 输出对应的id
     */
    @Override
    public JsonResult getAreaIdFromName(String name, Integer type) {
        List<Area> area = areaMapper.searchAreaByName(name, type);
        if(area.size() > 1) {
            throw new RuntimeException("出了错，数据库地址重复");
        }
        Integer id = area.get(0).getAreaId();
        if(id>0){
            return new JsonResult(0,"查询成功",id);
        }
        else {
            return new JsonResult(1,"查询失败",id);
        }
    }

    /**
     * 区域id获取区域信息
     * @param id 输入id查询city对象
     * @return
     */
    @Override
    public JsonResult getAreaById(Integer id, Integer type) {
        return new JsonResult<>(0, "", areaMapper.searchAreaById(id, type));
    }
}
